package com.hzit.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hzit.dto.StudentDTO;
import com.hzit.entity.PageResult;
import com.hzit.entity.R;
import com.hzit.entity.Student;
import com.hzit.service.IStudentService;
import com.hzit.service.impl.StudentServiceImpl;
import com.hzit.util.ResponseUtil;
import com.hzit.vo.StudentVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	private IStudentService studentService;
	@Override
	public void init() throws ServletException {
		studentService = new StudentServiceImpl();
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 对响应对象指定字符集编码
		resp.setContentType("application/json;charset=utf8");
		//2. 得到cmd请求参数
		String cmd = req.getParameter("cmd");
		//3. 根据参数的值决定调用不同的方法
		if(!Objects.isNull(cmd)){
			if("search".equals(cmd)){       // 条件查询带分页
				search(req,resp);
			}
			if("add".equals(cmd)){          // 添加学生
				add(req,resp);
			}
			if("update".equals(cmd)){       // 修改学生
				update(req,resp);
			}
			if("delete".equals(cmd)){       // 删除学生
				delete(req,resp);
			}
		}
	}
	// 删除学生
	private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 得到要删除的学生编号
		String sids = req.getParameter("sids");
		try {
			//2. 从数据库中删除学生
			studentService.delete(sids);
			//3. 构造r对象
			R<Object> r = R.ok(null,"删除学生成功！");
			//4. 输出
			ResponseUtil.out(resp,r);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.out(resp,R.fail("删除学生出错，原因是：" + e.getMessage()));
		}

	}

	// 修改学生
	private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 得到前端传入的要添加的学生数据
		ObjectMapper objectMapper = new ObjectMapper();
		StudentDTO dto = objectMapper.readValue(req.getInputStream(), StudentDTO.class);
		try {
			//2. 修改学生
			studentService.update(dto);
			//3. 构造r对象
			R<Object> r = R.ok(null,"修改学生成功！");
			//4. 输出
			ResponseUtil.out(resp,r);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.out(resp,R.fail("修改学生出错，原因是：" + e.getMessage()));
		}

	}
	// 添加学生
	private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 得到前端传入的要添加的学生数据
		ObjectMapper objectMapper = new ObjectMapper();
		StudentDTO dto = objectMapper.readValue(req.getInputStream(), StudentDTO.class);
		try {
			//2. 添加学生
			studentService.insert(dto);
			//3. 构造r对象
			R<Object> r = R.ok(null,"添加学生成功！");
			//4. 输出
			ResponseUtil.out(resp,r);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.out(resp,R.fail("添加学生出错，原因是：" + e.getMessage()));
		}

	}

	// 条件查询带分页
	private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 从前端取得当前页page及每页大小pageSize
		int page = Integer.parseInt(req.getParameter("page"));
		int pageSize = Integer.parseInt(req.getParameter("pageSize"));
		//2. 得到前端传入的条件查询参数对象
		ObjectMapper objectMapper = new ObjectMapper();
		StudentVO vo = objectMapper.readValue(req.getInputStream(), StudentVO.class);
		//3. 根据分页参数及条件参数查询得到分页结果对象
		PageResult<StudentDTO> pr = studentService.search(page,pageSize,vo);
		//4. 将上面的分页结果放到r对象中
		R<PageResult<StudentDTO>> r = R.ok(pr);
		//5. 将此数送到前端
		ResponseUtil.out(resp,r);
	}
}
